home *** CD-ROM | disk | FTP | other *** search
/ The X-Philes (2nd Revision) / The X-Philes Number 1 (1995).iso / xphiles / hp48_1 / ureduce < prev    next >
Internet Message Format  |  1995-03-31  |  4KB

  1. From comp.sys.handhelds Mon Feb 11 13:37:35 1991
  2. Path: mentor.cc.purdue.edu!noose.ecn.purdue.edu!samsung!uunet!decwrl!pa.dec.com!rust.zso.dec.com!shlump.nac.dec.com!jareth.enet.dec.com!edp
  3. From: edp@jareth.enet.dec.com (Eric Postpischil (Always mount a scratch monkey.))
  4. Newsgroups: comp.sys.handhelds
  5. Subject: Units
  6. Message-ID: <20016@shlump.nac.dec.com>
  7. Date: 8 Feb 91 21:29:31 GMT
  8. Sender: newsdaemon@shlump.nac.dec.com
  9. Reply-To: edp@jareth.enet.dec.com (Eric Postpischil (Always mount a scratch monkey.))
  10. Organization: Digital Equipment Corporation
  11. Lines: 124
  12.  
  13. Somebody asked for something like an opposite to UBASE -- a program that would
  14. convert a unit expression like 1_kg*m^2/s^3 to 1_W.  Here is something people
  15. can try out.  UREDUCE takes a unit object in level 2 and a list of preferred
  16. units in level 1.  It then tries to represent the given object in terms of the
  17. preferred units.
  18.  
  19. The method used to do this is fairly primitive.  For each preferred unit, the
  20. program tests to see whether multiplying or dividing the given unit by the
  21. preferred unit results in a reduced, or "better", unit expression.  "Better"
  22. is defined in this case as:  Of two unit expressions, the one with the lesser
  23. sum of the squares of the exponents of the UBASE units.  I.e., reducing the
  24. magnitude of the exponents makes a better expression, and reducing large
  25. exponents is better than reducing small exponents.  In effect, the program
  26. multiplies or divides by a preferred unit until the remaining expression does
  27. not become better, then it moves on to the next preferred unit.
  28.  
  29. This method has the advantge that 1_kg*m^2/s^4 and 1_kg*m/s^2 are converted,
  30. given a preference list of { 1_W }, to 1_W/s and 1_W*s, respectively -- the
  31. program is not stymied by negative values or by trying to make exact matches.
  32.  
  33. What order should you put things in the preference list?  The things you want
  34. to see most should go first.  Be aware that these are going to override things
  35. that come later.  If you have both a unit of power and a unit of energy in the
  36. list, chances are that the results will come out in terms of the first unit,
  37. even if the second unit is a better fit.  This program is really just a testing
  38. of the waters -- try it out and let me know what suggestions you have for
  39. improvements.  This program is also not optimized yet.  I suspect it might be
  40. faster to do the unit manipulations using arrays.
  41.  
  42. Another algorithm might be to have the user supply a set of 8 units (seven
  43. physical units plus one user-defined unit).  If the user-supplied 8 units were
  44. linearly independent, it would be possible to write a program that converted
  45. any given unit expression into an expression using the user-supplied units.
  46. This conversion would be exact, without the heuristic method used above.  I
  47. think it should be faster as well.  This still wouldn't convert complex unit
  48. expressions to things like energy or viscosity, but it would allow conversion to
  49. cgs or English base units.
  50.  
  51. The source is below.  It uses Bill Wickes' DIMS, so I am including that. 
  52.  
  53.  
  54.                 -- edp (Eric Postpischil)
  55.                 "Always mount a scratch monkey."
  56.                 edp@jareth.enet.dec.com
  57.  
  58. Download this, convert with ASC->, and store in 'DIMS':
  59.  
  60. %%HP: T(3)A(D)F(.);
  61. "D9D20D29512BF81B7040D9D2044EF0A211693045FC436ADB46AAC35F30403C37
  62. 088130E4A2070000FF40D35D53453392020000000000072102C230178A2CB916
  63. D9D20339202000000000006520189A2B21303223019D35433706B436AAC35442
  64. 30B2130B21303587"
  65.  
  66. Download:
  67.  
  68. %%HP: T(3)A(R)F(.);
  69. DIR
  70.   UREDUCE
  71.     \<< 1 \-> remain
  72. preferred result
  73.       \<< 1 preferred
  74. SIZE
  75.         FOR i
  76. 'result' preferred
  77. i GET remain OVER
  78. BEST ^ DUP 'remain'
  79. STO* STO/
  80.         NEXT result
  81. remain UBASE *
  82.       \>>
  83.     \>>
  84.   UGOOD
  85.     \<<
  86.       IF DUP TYPE
  87. 13 \=/
  88.       THEN DROP 0
  89.       ELSE DIMS 8
  90. \->ARRY ABS
  91.       END
  92.     \>>
  93.   BETTER
  94.     \<< \-> goodc good
  95. testc test
  96.       \<<
  97.         IF test
  98. good <
  99.         THEN testc
  100. test 1
  101.         ELSE goodc
  102. good 0
  103.         END
  104.       \>>
  105.     \>>
  106.   BESTM
  107.     \<< \-> input unit
  108.       \<< -1
  109.         WHILE unit
  110. OVER ^ input *
  111. UGOOD BETTER
  112.         REPEAT OVER
  113. 1 -
  114.         END
  115.       \>>
  116.     \>>
  117.   BESTP
  118.     \<< \-> input unit
  119.       \<< 1
  120.         WHILE unit
  121. OVER ^ input *
  122. UGOOD BETTER
  123.         REPEAT OVER
  124. 1 +
  125.         END
  126.       \>>
  127.     \>>
  128.   BEST
  129.     \<< \-> input unit
  130.       \<< 0 input
  131. UGOOD input unit
  132. BESTP input unit
  133. BESTM DROP
  134.       \>>
  135.     \>>
  136. END
  137.  
  138.